package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import android.view.View
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityMaterialUpBinding
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.MaterialViewModel
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import kotlinx.android.synthetic.main.activity_bank_up.*
import kotlinx.android.synthetic.main.activity_material_up.*
import kotlinx.android.synthetic.main.activity_material_up.etEqpWorkID
import kotlinx.android.synthetic.main.activity_material_up.tvPosition

/**
 * FileName: MaterialUpActivity
 * Author: XJH
 * Date: 2021/5/14 17:48
 * Description:
 * History:
 */
class MaterialUpActivity : BaseActivity<MaterialViewModel, ActivityMaterialUpBinding>(),
    View.OnClickListener {

    private var positionPop : PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_material_up
    }

    override fun initVariableId(): Int {
        return BR.materialViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("物料上机")
        viewModel.initViewModel()
        flPositionList.setOnClickListener(this)
        setRightListener("绑定"){
            if (tvPosition.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("物料上料位不能为空",false)
                return@setRightListener
            }
            viewModel.mLotKitting("ATTACH")
        }
    }

    override fun initData() {
        initEdit(etEqpWorkID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.eqpID = s
                viewModel.searchMaterial()
            }

        })

        viewModel.defUI.callObserve.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("物料上机成功",true)
            tvPosition.text = ""
            etEqpWorkID.setText("")
            etLot.setText("")

        }
    }

    override fun onClick(v: View?) {
        if (viewModel.positionList.size <= 0){
            viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到设备上料位信息",false)
            return
        }

        if (positionPop == null){
            positionPop = PopWindowsUtils.getOneChoosePop(this, flPositionList.width, viewModel.positionList, ivPositionArrow,object : PopWindowsUtils.PopTextAndPositionCallback{
                override fun onSuccess(codeS: String, potion: Int) {
                    tvPosition.text = codeS
                    viewModel.positionName = codeS
                }

            })

        }

        if (positionPop!!.isShowing) {
            positionPop?.dismiss()
        } else {
            PopWindowsUtils.setArrowShow(ivPositionArrow)
            positionPop?.showAsDropDown(flPositionList, 0, 0)
        }
    }
}